package org.rbac.serv;

import java.util.List;

import org.hibernate.Query;
import org.rbac.po.PermissionAction;
import org.rbac.vo.Action;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资源管理
 * 
 * <pre>
 * 项目名称：rbac
 * 类名称：ActionServices
 * 类描述：资源管理
 * 创建人：唐家林(tjl)
 * 创建时间：2014年9月28日　上午11:53:19
 * 修改人：
 * 修改时间：
 * 修改备注：
 * @version
 * </pre>
 */
@Service
public class ActionServices extends CommonServices {
    
	
	/**
	 * 修改
	 * 
	 * @param a
	 *            修改的数据
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Transactional(timeout = 20, propagation = Propagation.REQUIRED, readOnly = true)
	public List<PermissionAction> listAction() throws Exception {
		String hql = "SELECT pa FROM PermissionAction pa SET pa.AStatus = 1";
		return (List<PermissionAction>) getDao().queryForList(hql);
	}

	/**
	 * 修改
	 * 
	 * @param a
	 *            修改的数据
	 * @throws Exception
	 */
	@Transactional(timeout = 20, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void moidifyAction(Action a) throws Exception {
		this.editAction(a);
//		System.out.println(AopUtils.isAopProxy(this));
//		System.out.println(AopUtils.isCglibProxy(this)); //cglib
//		System.out.println(AopUtils.isJdkDynamicProxy(this)); //jdk动态代理
		//throw new Exception("xxxx");
	}
	
	/**
	 * 修改状态
	 * 
	 * @param ids
	 *            修改状态的数据主键
	 * @param status
	 *            状态(0:删除, 1:正常, 2:禁用)
	 * @throws Exception
	 */
	public void moidifyActionStatus(Integer[] ids, short status) throws Exception {
		String hql = "UPDATE PermissionAction pa SET pa.AStatus = :status WHERE pa.id IN (:ids)";
		Query q = getDao().getSession().createQuery(hql);
		q.setParameter("status", status);
		q.setParameterList("ids", ids);
		q.executeUpdate();
	}

	/**
	 * 编辑(新增或修改)
	 * 
	 * @param a
	 *            操作的数据
	 */
	public void editAction(Action a) {
		if (null == a) {
			return;
		}
		PermissionAction action = null;
		if (null == a.getId()) {
			action = new PermissionAction();
		} else {
			action = (PermissionAction) getDao().get(PermissionAction.class, a.getId());
		}
		// 属性值拷贝(拷贝r对象的属性值到role对象中对应名称的属性,且不拷贝id和RCdate的值)
		BeanUtils.copyProperties(a, action, new String[] { "id", "AStatus" });

		if (null == a.getId()) {
			short status = 1;
			action.setAStatus(status);
			getDao().save(action);
		} else {
			getDao().update(action);
		}
		
		
	}
}
